import type { RouteRecordRaw } from 'vue-router'
import type { Menu } from '@/typings/menu'
import { PageNames } from './pagenames'
import { Pages } from '../modules/index'
import { router } from '../index'

export function getPageDef(name: string) {
    const page = Pages[name]
    if (page != null) {
        page.name = name
        return page
    }
    console.error(`未找到name为${name}的页面`)
    return null
}

const HOME_ROUTE: RouteRecordRaw = {
    name: 'root',
    path: '/',
    redirect: import.meta.env.VITE_ROUTE_HOME_PATH,
    meta: {
        title: 'Root'
    }
}

function initConstantRoutes() {
    const basicLayout = {
        name: PageNames.LAYOUT_BASIC,
        path: '/',
        component: () => import('@/layouts/basic-layout/index.vue'),
        children: [] as any[],
        props: true
    }
    const blankLayout = {
        name: PageNames.LAYOUT_BLANK,
        path: '/',
        component: () => import('@/layouts/blank-layout/index.vue'),
        children: [] as any[],
        props: true
    }

    const modules: any = import.meta.glob('../modules/**/*.ts', {
        eager: true
    })
    const { VITE_SERVICE_ENV = 'dev' } = import.meta.env
    for (const path in modules) {
        if (VITE_SERVICE_ENV != 'dev' && path == './builtin/demo.ts') {
            //非开发模式，要忽略demo里的配置
            continue
        }
        const items = modules[path].default
        if (typeof items == 'object') {
            Object.keys(items).forEach((key) => {
                const item = items[key]
                if (item.meta?.autoMount) {
                    //配置了自动挂载的，已经在router/routes/index.ts中处理了
                    const route = { name: key, ...item }
                    //配置的自动挂载
                    const layout = item.meta?.layout ?? PageNames.LAYOUT_BASIC
                    switch (layout) {
                        case PageNames.LAYOUT_BASIC:
                            basicLayout.children.push(route)
                            break
                        case PageNames.LAYOUT_BLANK:
                            blankLayout.children.push(route)
                            break
                        default:
                            throw new Error(
                                `${key}对应的route配置了未知的layout:${layout}`
                            )
                    }
                }
            })
        }
    }

    return [HOME_ROUTE, basicLayout, blankLayout]
}

/** 固定的路由 */
const constantRoutes: RouteRecordRaw[] = initConstantRoutes()

/**
 * 添加页面到basic布局下
 * @param name
 */
function addPageToBasic(menu: Menu) {
    let pageDef = getPageDef(menu.permissionKey)
    if (!pageDef) return
    pageDef = { ...pageDef }
    pageDef.path = menu.url!
    pageDef.meta = { ...pageDef.meta, icon: menu.icons, title: menu.name }
    router.addRoute(PageNames.LAYOUT_BASIC, pageDef)
}
/**
 * 添加页面到iframe局下
 * @param name
 */
function addPageToIframe(menu: Menu) {
    let pageDef = getPageDef(PageNames.LAYOUT_IFRAME)
    if (!pageDef) return
    pageDef = { ...pageDef }
    pageDef.name = menu.permissionKey
    pageDef.path = menu.routeUrl!
    pageDef.meta = { ...pageDef.meta, icon: menu.icons, title: menu.name }
    router.addRoute(PageNames.LAYOUT_BASIC, pageDef)
}

/**
 * 添加页面到blank布局下
 * @param name
 */
function addPageToBlank(menu: Menu) {
    let pageDef = getPageDef(menu.permissionKey)
    if (!pageDef) return
    pageDef = { ...pageDef }
    pageDef.path = menu.url!
    pageDef.meta = { ...pageDef.meta, icon: menu.icons, title: menu.name }
    router.addRoute(PageNames.LAYOUT_BLANK, pageDef)
}

export { constantRoutes, addPageToBasic, addPageToBlank, addPageToIframe }
